import { login, sysProfile, sysUser } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'
const state = {
  // 存储token
  token: getToken() || '',
  // 用户信息
  userInfo: ''
}
const mutations = {
  // 修改token
  setToken(state, value) {
    state.token = value
    setToken(value)
  },
  // 修改用户信息
  setUserInfo(state, value) {
    state.userInfo = value
  },
  // 删除token
  logout(state) {
    state.token = ''
    setToken('')
    // 用户信息也要清空
    state.userInfo = ''
  }
}
const actions = {
  // 登录成功接口调用
  async toLogin({ commit }, value) {
    const res = await login(value)
    commit('setToken', res.data)
    console.log(res)
  },
  // 获取用户信息
  async getUserInfo({ commit }) {
    const res = await sysProfile()
    // 获取头像接口调用
    const res2 = await sysUser(res.data.userId)
    // commit('setUserInfo', res.data)
    commit('setUserInfo', { ...res.data, ...res2.data })
    console.log('用户信息', res, res2)
  }
}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
